package me.shurufa.holder;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.activities.BookDetailActivity;
import me.shurufa.activities.MyBookCollectDetailActivity;
import me.shurufa.activities.MyBookShelfDetailActivity;
import me.shurufa.activities.MyCommentDetailActivity;
import me.shurufa.bean.Book;
import me.shurufa.implement.BookLongClickListener;
import me.shurufa.utils.Constants;

/**
 * 发现页图书列表ViewHolder
 *
 * @FileName: me.shurufa.holder.DiscoverBookViewHolder.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-21 18:55
 */
public class MostDigestViewHolder extends BaseViewHolder implements View.OnLongClickListener {

    @Bind(R.id.book_img)
    ImageView bookImage;
    @Bind(R.id.book_name)
    TextView bookTitle;
    @Bind(R.id.tv_digest_num)
    TextView tv_digest_num;

    private Context mContext;
    private Book mBook;
    private int fromWhere;
    private long userId;
    private boolean mShowCommentNum;
    private BookLongClickListener listener;

    public MostDigestViewHolder(View itemView, Object parent, int fromWhere, long uid, BookLongClickListener listener) {
        this(itemView, parent, fromWhere, uid);
        this.listener = listener;
        if (fromWhere == Constants.TYPE_FROM_MY_BOOKSHELVES)
            itemView.setOnLongClickListener(this);
    }

    public MostDigestViewHolder(View itemView, Object parent, int fromWhere, long uid, boolean showCommentNum, BookLongClickListener listener) {
        this(itemView, parent, fromWhere, uid);
        this.listener = listener;
        mShowCommentNum = showCommentNum;
        if (fromWhere == Constants.TYPE_FROM_MY_BOOKSHELVES)
            itemView.setOnLongClickListener(this);
    }


    public MostDigestViewHolder(View itemView, Object parent, int fromWhere, long uid) {
        this(itemView, parent);
        this.fromWhere = fromWhere;
        this.userId = uid;
        if (fromWhere == Constants.TYPE_FROM_MY_BOOKSHELVES) {
            bookImage.setOnLongClickListener(this);
            bookTitle.setOnLongClickListener(this);
        }

    }

    public MostDigestViewHolder(View itemView, Object parent) {
        super(itemView, parent);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        bookTitle.setMaxLines(2);
        addOnClickListener(bookImage);
        addOnClickListener(bookTitle);

    }

    public void bind(Book book) {
        mBook = book;

        // 设置tag
        /*if (mBook != null && !TextUtils.isEmpty(mBook.image)) {
            bookImage.setTag(R.string.app_name, mBook.image);
        }*/

        // 书评个数
        if (Constants.TYPE_FROM_MOST_COMMENT == fromWhere || Constants.TYPE_FROM_MY_COMMENT == fromWhere) {
            tv_digest_num.setText(mBook.comment_num + mContext.getString(R.string.comment_num));
        }
        // 书摘个数
        else {
            tv_digest_num.setText(mBook.excerpt_num + mContext.getString(R.string.digest_num));
        }

        if (Constants.TYPE_FROM_BOOK_RECENT == fromWhere) {
            if (0 == mBook.excerpt_num && 0 == mBook.comment_num) {
                // 后台错误 不应该出现这种
            } else if (0 == mBook.excerpt_num && 0 != mBook.comment_num) {
                tv_digest_num.setText(mBook.comment_num + mContext.getString(R.string.comment_num));
            }
        }

        if (!TextUtils.isEmpty(mBook.title)) {
            bookTitle.setText(mBook.title);
        }

        Glide.with(mContext).load(mBook.image).placeholder(R.drawable.ic_book_loading).error(R.drawable.ic_book_loading).crossFade(600).into(bookImage);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.book_img:
            case R.id.book_name:
                PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.9f, 1f);
                PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.9f, 1f);
                ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(bookImage, scaleX, scaleY);
                animator.setDuration(150);
                animator.setInterpolator(new LinearInterpolator());
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        handleClick();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animator.start();
                break;
        }
    }

    private void handleClick() {
        Intent intent = null;
        // 关闭输入法
        EventBus.getDefault().post(new Intent(Constants.EVENT_CLOSE_INPUT));

        switch (fromWhere) {
            // 跳转至图书详情界面
            case Constants.TYPE_FROM_MOST_DIGEST:
            case Constants.TYPE_FROM_BOOK_RECENT:
                intent = new Intent(mContext, BookDetailActivity.class);
                if (mBook.id != 0L) {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.id);
                } else {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.bid);
                }
                intent.putExtra(Constants.ARG_FROM_WHERE, fromWhere);
                mContext.startActivity(intent);
                break;

            // 跳转至图书详情界面
            case Constants.TYPE_FROM_MOST_COMMENT:
                intent = new Intent(mContext, BookDetailActivity.class);
                if (mBook.id != 0L) {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.id);
                } else {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.bid);
                }
                intent.putExtra(Constants.ARG_FROM_WHERE, fromWhere);
                mContext.startActivity(intent);
                break;

            // 从我的书架进的
            case Constants.TYPE_FROM_MY_BOOK_SHELF:
                intent = new Intent(mContext, MyBookShelfDetailActivity.class);
                if (mBook.id != 0L) {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.id);
                } else {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.bid);
                }
                intent.putExtra(Constants.ARG_FROM_WHERE, fromWhere);
                mContext.startActivity(intent);
                break;

            // 从我的书评进的
            case Constants.TYPE_FROM_MY_COMMENT:
                intent = new Intent(mContext, MyCommentDetailActivity.class);
                if (mBook.id != 0L) {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.id);
                } else {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.bid);
                }
                intent.putExtra(Constants.ARG_FROM_WHERE, fromWhere);
                mContext.startActivity(intent);
                break;

            // 从我的收藏进的
            case Constants.TYPE_FROM_MY_COLLECT:
                intent = new Intent(mContext, MyBookCollectDetailActivity.class);
                if (mBook.id != 0L) {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.id);
                } else {
                    intent.putExtra(Constants.ARG_BOOK_ID, mBook);//mBook.bid);
                }
                intent.putExtra(Constants.ARG_FROM_WHERE, fromWhere);
                mContext.startActivity(intent);
                break;

        }
    }

    @Override
    public boolean onLongClick(View v) {
        /*if (listener != null && !mBook.isBookShelvesFavourite) {
            listener.onBookLongClick(v, mBook);
        }*/
        return true;
    }

}
